/**
 * @author hewei
 * @version 1.0
 * @description: 949. 给定数字能组成的最大时间
 * @date 2022/9/22 15:08
 */

public class LargestTimeFromDigits {

    public String largestTimeFromDigits(int[] arr) {
        int n = arr.length;
        int ans = -1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (j == i) continue;
                for (int k = 0; k < n; k++) {
                    if (k == j || k == i) continue;
                    int l = 6 - i - j - k;
                    int hour = 10 * arr[i] + arr[j];
                    int minis = 10 * arr[k] + arr[l];
                    int temp = hour * 60 + minis;
                    if (hour < 24 && minis < 60 && temp > ans) ans = temp;
                }
            }
        }
        return ans >= 0 ? String.format("%02d:%02d", ans / 60, ans % 60) : "";
    }
}